package com.hotel.front;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.DepartmentLogic;

import data.Department;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class DepartmentService extends HttpServlet{
	//http://localhost:8080/departmentService?func=addDepartment&title=1&shortname=1&description=aa
	//http://localhost:8080/departmentService?func=editDepartment&departmentId=848&title=2&shortname=2&description=aabbccdd	
	//http://localhost:8080/departmentService?func=delDepartment&departmentId=848
	//http://localhost:8080/departmentService?func=changeDepartment&departmentId=848
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addDepartment")){
				AddDepartment(req,resp);
			}else if(doClass.equals("editDepartment")){
				EditDepartment(req,resp);
			}else if(doClass.equals("delDepartment")){
				DeleteDepartment(req,resp);
			}else if(doClass.equals("changeDepartment")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("getAllDepartment")){
				GetAllDepartment(req,resp);
			}else if(doClass.equals("getDepartmentById")){
				GetDepartmentById(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void GetAllDepartment(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		Object obj = new DepartmentLogic().GetAllDepartmentAdminLimit(hotelId,offset,line);
		double dataSize = new DepartmentLogic().GetAllDepartment(hotelId).size();
		Gson gson = new Gson();
		List<Object> rsObj = Arrays.asList(dataSize,pointer,line,obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(rsObj));
	}
	private void GetDepartmentById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		Department obj = new DepartmentLogic().GetDepartment(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void AddDepartment(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		String shortTitle = req.getParameter("shortTitle");
		String description = req.getParameter("description");

		Object obj = new DepartmentLogic().AddDepartment(hotelId,title,shortTitle,description);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

	private void EditDepartment(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long departmentId = Long.parseLong(req.getParameter("departmentId"));
		String title = req.getParameter("title");
		String shortTitle = req.getParameter("shortTitle");
		String description = req.getParameter("description");
				
		Object obj = new DepartmentLogic().EditDepartment(departmentId,title,shortTitle,description);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));

	}

	private void DeleteDepartment(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String departmentId = req.getParameter("departmentId");
		for(int i = 0; i < departmentId.split(",").length ; i++){
			new DepartmentLogic().DeleteDepartment(Long.parseLong(departmentId.split(",")[i]));
		}
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(true));
	}

	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long departmentId = Long.parseLong(req.getParameter("departmentId"));

		Object obj = new DepartmentLogic().ChangeStatus(departmentId);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
}
